using System;
using System.Collections;
using UnityEngine;

public class PassiveDailyCheck : MonoBehaviour
{
	public const string PLAYER_PREFS_LAST_CONNECTION = "LastConnection";

	private IEnumerator Start()
	{
		yield return new WaitForEndOfFrame();
		yield return new WaitForEndOfFrame();
		CheckDailyReward();
	}

	public void CheckDailyReward()
	{
		if (PlayerPrefs.HasKey("LastConnection"))
		{
			DateTime utcNow = DateTime.UtcNow;
			DateTime t = GetLastTime().AddDays(1.0);
			if (t < utcNow)
			{
				AchievementManager.Instance.IncrementPersistentData(AchievementKeys.DaysPlayed, 1);
			}
		}
		SetLastTimeAsNow();
	}

	private void OnApplicationFocus(bool isFocus)
	{
		if (isFocus)
		{
			CheckDailyReward();
		}
	}

	private void OnApplicationPause(bool paused)
	{
		if (!paused)
		{
			CheckDailyReward();
		}
	}

	private void OnApplicationQuit()
	{
		SetLastTimeAsNow();
	}

	private void SetLastTimeAsNow()
	{
		PlayerPrefs.SetString("LastConnection", DateTime.UtcNow.ToString());
	}

	private DateTime GetLastTime()
	{
		return DateTime.Parse(PlayerPrefs.GetString("LastConnection", DateTime.UtcNow.ToString()));
	}
}
